//
//  InventoryViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/5/6.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "InventoryViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "InventoryTableViewCell.h"
#import "InventoryDetailsViewController.h"
#import "YoungAllData.h"
@interface InventoryViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,YoungDelegate>
{
    UIScrollView *zScrollView;
    UITableView *zTableView;
}
@property(nonatomic,retain)NSMutableArray *zArrayData;
@end

@implementation InventoryViewController
@synthesize zArrayData;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden=YES;
    
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"库存";
    
    //[self initUI];
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data productWarning:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"]];
}
#pragma mark - 查看缺货回调
-(void)productWarning:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zArrayData = [[NSMutableArray alloc]init];
    zArrayData = [sign objectForKey:@"data"];
    [self initUI:[sign objectForKey:@"count"]];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 查看缺货回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark - initUI
-(void)initUI:(NSString *)count{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, 120)];
    backView.backgroundColor = [UIColor whiteColor];
    backView.userInteractionEnabled = YES;
    [self.view addSubview:backView];
    
    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 30)];
    leftLabel.text = @"选择类别";
    leftLabel.textAlignment = NSTextAlignmentLeft;
    leftLabel.textColor = [UIColor blackColor];
    leftLabel.font = [UIFont systemFontOfSize:16];
    [backView addSubview:leftLabel];
    
    zScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, leftLabel.bottom, SCREEN_WIDTH, 80)];
    zScrollView.userInteractionEnabled = YES;
    [backView addSubview:zScrollView];
    
    zScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 80);
    
    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"查看库存_07",@"查看库存_09",@"查看库存_11", nil];
    for (int i = 0 ; i < 3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(30+i*100, 0, 60, 75);
        [btn setBackgroundImage:[UIImage imageNamed:[array objectAtIndex:i]] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        [zScrollView addSubview:btn];
    }
    
    UIView *belowView = [[UIView alloc]initWithFrame:CGRectMake(0, backView.bottom+10, SCREEN_WIDTH, SCREEN_HEIGHT-140-64-50)];
    belowView.backgroundColor = [UIColor whiteColor];
    belowView.userInteractionEnabled = YES;
    [self.view addSubview:belowView];
    
    UILabel *queLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 64, 30)];
    queLabel.text = @"缺货提示";
    queLabel.textAlignment = NSTextAlignmentLeft;
    queLabel.textColor = [UIColor blackColor];
    queLabel.font = [UIFont systemFontOfSize:16];
    [belowView addSubview:queLabel];

    UILabel *queLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(queLabel.right, 5, 150, 30)];
    queLabel1.text = [NSString stringWithFormat:@"(库存低于%@台)",count];
    queLabel1.textAlignment = NSTextAlignmentLeft;
    queLabel1.textColor = [UIColor grayColor];
    queLabel1.font = [UIFont systemFontOfSize:14];
    [belowView addSubview:queLabel1];
    
    zTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,queLabel.bottom+5, SCREEN_WIDTH, belowView.frame.size.height-60+10)];
    //zTableView.backgroundColor = [UIColor redColor];
    zTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    zTableView.delegate = self;
    zTableView.dataSource = self;
    [belowView addSubview:zTableView];
    
    
    // [zTableView registerClass:[InventoryViewController class] forCellReuseIdentifier:@"cell"];
    
    
}
#pragma mark - 分类库存
-(void)pressBtn:(UIButton *)sender{
    InventoryDetailsViewController *details = [[InventoryDetailsViewController alloc]init];
    if (sender.tag == 0) {
         details.categoryIDStr = [NSString stringWithFormat:@"%d", 10];
    }else if (sender.tag == 1){
         details.categoryIDStr = [NSString stringWithFormat:@"%d", 12];
    }else{
         details.categoryIDStr = [NSString stringWithFormat:@"%d", 16];
    }
   
    switch (sender.tag) {
        case 0:
            details.titleStr = @"库存-护肤仪器";
            break;
        case 1:
            details.titleStr = @"库存-化妆品";
            break;
        default:
            details.titleStr = @"库存-美妆工具";
            break;
    }
    [self.navigationController pushViewController:details animated:YES];
}
#pragma mark - UitableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [zArrayData count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InventoryTableViewCell *cell = [[InventoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    cell.zName.text = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"ProductName"]];
    cell.zNumber.text = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"Stock"]];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
